package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.FolderPO;
import com.ajmd.onlineexam.entity.QuestionBankPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.entity.TestPaperPO;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.FolderService;
import com.ajmd.onlineexam.service.TestPaperService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ajmd
 * @since 2023/4/4 19:55
 */

@Api("试卷接口")
@RestController
@RequestMapping("/testPaper")
public class TestPaperController {
    @Resource
    private TestPaperService testPaperService;
    @Resource
    private FolderService folderService;

    @PostMapping("/add")
    public CommonResult addTestPaper(@RequestBody TestPaperPO testPaperPO){
        if(ObjectUtils.isEmpty(testPaperPO.getTestPaperName()) || ObjectUtils.isEmpty(testPaperPO.getBelongFolderId())){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }

        FolderPO folder= folderService.findFolderById(testPaperPO.getBelongFolderId());
        if(ObjectUtils.isEmpty(folder)){
            return CommonResult.error(AppExceptionCodeEnum.FOLDER_NOT_EXISTS);
        }
        int num = testPaperService.addTestPaper(testPaperPO);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.INSERT_TEST_PAPER_ERR);
        }
        return CommonResult.ok().message("新建试卷成功！");
    }


    @ApiOperation("查询分类下的试卷")
    @PostMapping("/findPart")
    public CommonResult findQuestionBanks(@MultiRequestBody String belongFolderId,
                                          @MultiRequestBody String account,
                                          @MultiRequestBody Integer userType,
                                          @MultiRequestBody Integer folderRange,
                                          @MultiRequestBody Integer currentPage,
                                          @MultiRequestBody Integer pageSize,
                                          @MultiRequestBody SearchParam searchParam){
        if(ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(userType) || ObjectUtils.isEmpty(folderRange)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        Page<TestPaperPO> pageData = testPaperService.findByFolder(belongFolderId, account, searchParam, userType, folderRange, currentPage, pageSize);
        return CommonResult.ok().data("pageData",pageData);
    }

    @PutMapping("/update")
    public CommonResult updateQuestionBankByPO(@RequestBody TestPaperPO testPaperPO){
        int num = testPaperService.testPaperUpdate(testPaperPO);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_TEST_PAPER_ERR);
        }
        return CommonResult.ok("试卷编辑成功");
    }

    @PutMapping("/move")
    public CommonResult batchUpdateBelongFolder(@MultiRequestBody List<String> testPaperIds,
                    @MultiRequestBody TestPaperPO testPaperPO,
                    @MultiRequestBody String account,@MultiRequestBody Integer userType){
        if(ObjectUtils.isEmpty(testPaperPO.getBelongFolderId()) || ObjectUtils.isEmpty(testPaperPO.getBelongFolderFullId())
                || ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        FolderPO folder = folderService.findFolderById(testPaperPO.getBelongFolderId());
        if(ObjectUtils.isEmpty(folder)){
            return CommonResult.error(AppExceptionCodeEnum.FOLDER_NOT_EXISTS);
        }
        int num = testPaperService.testPaperMove(testPaperIds,testPaperPO,account,userType);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.MOVE_TEST_PAPER_ERR)
                    .appendMessage("非试卷创建人无法移动！");
        }
        return CommonResult.ok().message("试卷移动成功！"+num+"张试卷被移动！");
    }


    @PostMapping("/delete")
    public CommonResult deleteQuestionBankByIds(@MultiRequestBody List<String> testPaperIds,
                                                @MultiRequestBody String account,@MultiRequestBody Integer userType){
        if(ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        int num = testPaperService.testPaperDelete(testPaperIds,account,userType);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.DELETE_TEST_PAPER_ERR)
                    .appendMessage("非试卷创建人无法删除！");
        }
        return CommonResult.ok().message("试卷删除成功！"+num+"条记录被删除！");
    }

}
